/**
 * AssessItem
 * @Author Ace.c
 * @Create 2016-12-28 15:54
 */
class AssessItem extends BaseSprite {

    private lab_rank: eui.Label;
    private lab_roomid: eui.Label;
    private item_1: AssessPlayerItem;
    private item_2: AssessPlayerItem;
    private item_3: AssessPlayerItem;
    private btn_change: eui.Button;
    private lab_time: eui.Label;

    constructor() {
        super();

        this.skinName = "AssessItemSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.btn_change.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_change:
                let changePanel: AssessChangePanel = gameManager.find('AssessChangePanel');
                if (changePanel) {
                    let data: any = {};
                    let obj: any = {};
                    obj.id = this.data.id;
                    obj.data = data;
                    let list: any;
                    let person: any;
                    for (let key in this.data) {
                        list = this.data[key];
                        if (typeof list == "object" && key != game.player.uid) {
                            person = {};
                            person.uid = key;
                            person.nick = list[0];
                            person.zan = list[1];
                            person.pic = list[2];
                            person.id = this.data.id;

                            data[person.uid] = person;
                        }
                    }
                    changePanel.show();
                    changePanel.update(obj);

                    game.isShowAsscess = true;
                }
                break;
        }
    }

    update(data: any, rank: number) {
        this.data = data;

        this.lab_rank.text = "" + rank;
        this.lab_roomid.text = "" + data.roomid;
        this.lab_time.text = "" + this.timeStr;

        let list: any[] = [];
        let player: any;
        for (let key in data) {
            if (typeof data[key] == "object" && +key > 0 && key != game.player.uid) {
                player = {};
                player.uid = key;
                player.nick = data[key][0];
                player.zan = data[key][1];

                list.push(player);
            }
        }


        let item: AssessPlayerItem;
        for (let i: number = 1; i <= 3; i++) {
            item = this['item_' + i];

            player = list[i - 1];

            if (player) {
                item.update(player);
                item.owner = player.uid == this.data.owner;
            }
            else {
                item.clean();
            }
        }

        switch (this.data.roomtype) {
            case 1:
                this.skinState = 'daikai';
                break;
            case 2:
                this.skinState = 'circle';
                break;
            case 3:
            default:
                this.skinState = 'normal';
                break;
        }


        this.btn_change.visible = rank == 1;
    }

    get timeStr() {
        return StringUtils.getYTDByTimestamp(this.data.time * 1000) + " " + StringUtils.getHMSByTimestamp(this.data.time * 1000);
    }
}